package ext.opensource.netty.common.api;

import ext.opensource.netty.common.core.CacheList;
import ext.opensource.netty.common.core.CacheListLocalMemory;
import ext.opensource.netty.common.core.UniqueIdInteger;

public class GlobalUniqueIdImpl implements GlobalUniqueId {
	private CacheList<UniqueIdInteger> list = new CacheListLocalMemory<UniqueIdInteger>();

	@Override
	public void setCacheList(final CacheList<UniqueIdInteger> cacheList) {
		this.list = cacheList;
	}

	@Override
	public int getNextMessageId(final String clientId) {
		UniqueIdInteger value = list.get(clientId);
		if (value == null) {
			value = new UniqueIdInteger();
		} else {
			value.addInc();
		}
		list.put(clientId, value);
		return value.id();
	}
}
